/**MenuItem
 *Item of the native menu
 *constructor: 
 *MenuItem(String text, Pixmap background)
 *@text Text will be show in, allow null.
 *@background the item background, allow null*/

package androidgames.networkgames.screens.components;

import java.util.ArrayList;

import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Pixmap;

public class MenuItem {
	public String text = "";
	public Pixmap background;
	public int iX, iTX, iTY;
	public int iY;
	public int width;
	public int height;
	public int iWidth;
	public int iHeight;

	public MenuItem(String text, Pixmap background) {
		this.text = text;
		this.background = background;
		iX = 0;
		iY = 0;
		width = 0;
		height = 0;
		iTX = 0;
		iTY = 0;
	}

	public boolean isTouched(int iTouchX, int iTouchY) {
		if (iTouchX >= iX && iTouchX <= iX + iWidth && iTouchY >= iY
				&& iTouchY <= iY + iHeight)
			return true;
		return false;
	}

	public static interface IEventListener {
		public void onTouch(MenuItem menuItem, TouchEvent touchevents);
	}

	private ArrayList<IEventListener> onTouchListeners = new ArrayList<IEventListener>();

	public void addTouchListener(IEventListener listener) {
		onTouchListeners.add(listener);
	}

	public void removeTouchListener(IEventListener listener) {
		onTouchListeners.remove(listener);
	}

	public void fireEvent(TouchEvent event) {
		IEventListener[] listeners = new IEventListener[1];
		listeners = onTouchListeners.toArray(listeners);
		if (listeners.length < 0)
			return;
		for (IEventListener listener : listeners) {
			listener.onTouch(this, event);
		}
	}

	public boolean update(float deltatime, TouchEvent touchevents) {
		// TODO Auto-generated method stub

		if (touchevents.type == TouchEvent.TOUCH_UP) {
			if (touchevents.x > this.iX && touchevents.y > this.iY
					&& touchevents.x < this.iX + this.iWidth
					&& touchevents.y < this.iY + this.iHeight) {
				TouchEvent cevent = new TouchEvent();
				cevent.type = TouchEvent.TOUCH_UP;
				fireEvent(cevent);
				this.text = "touch";
				return true;
			}
		}
		return false;
	}

	public void present(float deltatime, Graphics g) {

	}

}